package com.imufe.chemicalenterprisedangerwarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.imufe.chemicalenterprisedangerwarning.entity.po.EnterpriseDepartmentPO;
import com.imufe.chemicalenterprisedangerwarning.entity.query.EnterpriseDepartmentQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.EnterpriseDepartmentVO;
import com.imufe.chemicalenterprisedangerwarning.mapper.EnterpriseDepartmentMapper;
import com.imufe.chemicalenterprisedangerwarning.service.DepartmentService;
import com.imufe.chemicalenterprisedangerwarning.service.EnterpriseDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imufe.chemicalenterprisedangerwarning.util.BeanUtils;
import com.imufe.chemicalenterprisedangerwarning.util.ListUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 企业部门表 服务实现类
 * </p>
 *
 * @author BaiBan
 * @since 2022-09-24
 */
@Log4j2
@Service
public class EnterpriseDepartmentServiceImpl extends ServiceImpl<EnterpriseDepartmentMapper, EnterpriseDepartmentPO> implements EnterpriseDepartmentService {

    @Autowired
    EnterpriseDepartmentMapper mapper;

    @Autowired
    DepartmentService departmentService;

    @Override
    public List<EnterpriseDepartmentVO> list(Long enterpriseId) {
        QueryWrapper<EnterpriseDepartmentPO> qw = new QueryWrapper<>();
        qw.eq("enterprise_id", enterpriseId);
        List<EnterpriseDepartmentPO> list = mapper.selectList(qw);

        return list
                .stream()
                .map((po) -> this.detailed(po.getId()))
                .toList();
    }

    @Override
    public EnterpriseDepartmentVO detailed(Long id) {
        EnterpriseDepartmentPO po = mapper.selectById(id);
        log.info(po);
        EnterpriseDepartmentVO vo = BeanUtils.copyPOJO(po, EnterpriseDepartmentVO.class);
        vo.setDepartment(departmentService.detailed(po.getDepartmentId()));
        return vo;
    }

    @Override
    public EnterpriseDepartmentVO create(EnterpriseDepartmentQuery query) {
        EnterpriseDepartmentPO po = BeanUtils.copyPOJO(query, EnterpriseDepartmentPO.class);
        mapper.insert(po);
        log.info(po);
        return this.detailed(po.getId());
    }

    @Override
    public EnterpriseDepartmentVO update(EnterpriseDepartmentQuery query) {
        EnterpriseDepartmentPO po = BeanUtils.copyPOJO(query, EnterpriseDepartmentPO.class);
        mapper.updateById(po);
        log.info(po);
        return this.detailed(po.getId());
    }
}
